using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace FileManager.Models
{
    public class AppSettings
    {
        public void UpdateFrom(AppSettings newSettings)
        {
            this.ScanDirectories = newSettings.ScanDirectories;
            this.AutoScanOnStartup = newSettings.AutoScanOnStartup;
            this.DeleteToRecycleBin = newSettings.DeleteToRecycleBin;
            this.ShowNotifications = newSettings.ShowNotifications;
            this.MaxScanThreads = newSettings.MaxScanThreads;
            this.ThumbnailSize = newSettings.ThumbnailSize;
        }
        private static readonly string SettingsPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
            "WeChatFileManager",
            "settings.json");
            
        public List<string> ScanDirectories { get; set; } = new List<string>();
        public bool AutoScanOnStartup { get; set; } = false;
        public bool DeleteToRecycleBin { get; set; } = true;
        public bool ShowNotifications { get; set; } = true;
        public int MaxScanThreads { get; set; } = 4;
        public int ThumbnailSize { get; set; } = 100;
        
        public static AppSettings Load()
        {
            try
            {
                // 确保目录存在
                Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath));
                
                if (File.Exists(SettingsPath))
                {
                    string json = File.ReadAllText(SettingsPath);
                    return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载设置时出错: {ex.Message}");
            }
            
            return new AppSettings();
        }
        
        public void Save()
        {
            try
            {
                // 确保目录存在
                Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath));
                
                string json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
                File.WriteAllText(SettingsPath, json);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存设置时出错: {ex.Message}");
            }
        }
        
        public void AddDefaultWeChatDirectories()
        {
            // 添加默认的微信文件目录
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string wechatFilesPath = Path.Combine(documentsPath, "WeChat Files");
            
            if (Directory.Exists(wechatFilesPath) && !ScanDirectories.Contains(wechatFilesPath))
            {
                ScanDirectories.Add(wechatFilesPath);
            }
            
            // 尝试添加其他可能的微信文件目录
            string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string tencent = Path.Combine(appDataPath, "Tencent");
            string wechat = Path.Combine(tencent, "WeChat");
            string wechatFiles = Path.Combine(wechat, "WechatFiles");
            
            if (Directory.Exists(wechatFiles) && !ScanDirectories.Contains(wechatFiles))
            {
                ScanDirectories.Add(wechatFiles);
            }
        }
    }
}